home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / c / cxref-1.001 / cxref-1~ / cxref / memory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-24  |  9.8 KB  |  437 lines

  1. /***************************************
  2.   $Header: /home/amb/cxref/RCS/memory.c 1.6 1996/02/24 14:53:27 amb Exp $
  3.  
  4.   C Cross Referencing & Documentation tool. Version 1.0
  5.  
  6.   Memory management functions
  7.   ******************/ /******************
  8.   Written by Andrew M. Bishop
  9.  
  10.   This file Copyright 1995,96 Andrew M. Bishop
  11.   It may be distributed under the GNU Public License, version 2, or
  12.   any higher version.  See section COPYING of the GNU Public license
  13.   for conditions under which this file may be redistributed.
  14.   ***************************************/
  15.  
  16. /*+ The amount of debugging, 1 for totals, 2 for logging, 4 for printing each call. +*/
  17. #define DEBUG 0
  18.  
  19. /*+ Under Linux, stdarg is used +*/
  20. #if defined(__linux__)
  21. #define USE_STD_ARG
  22. #endif
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27.  
  28. #ifdef USE_STD_ARG
  29. #include <stdarg.h>
  30. #else
  31. #include <varargs.h>
  32. #endif
  33.  
  34. #include <memory.h>
  35. #include "memory.h"
  36.  
  37. /*+ A private memory heap is used to reduce the number of malloc calls that are made, the Heap type is a pointer to this. +*/
  38. typedef struct _Heap *Heap;
  39.  
  40. /*+ A structure containing all of the information about the private heap in a linked list. +*/
  41. struct _Heap
  42. {
  43.  char* mem;                             /*+ The memory that is private to the heap. +*/
  44.  Heap next;                             /*+ The next Heap structure. +*/
  45. };
  46.  
  47. /*+ Local variable to control the usage of the private heap; +*/
  48. static Heap first=NULL;                 /*+ the first segment of memory on the private heap. +*/
  49. static int heap_left=0;                 /*+ the amount of space left in the current heap segment. +*/
  50.  
  51. static char* get_space(unsigned int l);
  52. static Heap add_to_heap(unsigned int l);
  53.  
  54. #if DEBUG&2
  55. /*+ Variable used for debugging, not a good thing to do. what if more thatn 8192 mallocs?+*/
  56. static void* addresses[8192];
  57. static char* files[8192];
  58. static int   lines[8192];
  59. #endif
  60. #if DEBUG
  61. /*+ Variable used for debugging. +*/
  62. static int malloc_count=0;
  63. static int realloc_count=0;
  64. static int free_count=0;
  65. #endif
  66.  
  67.  
  68. /*++++++++++++++++++++++++++++++++++++++
  69.   A replacement malloc() function.
  70.  
  71.   void* SafeMalloc Returns the address.
  72.  
  73.   unsigned int size The size of the memory to allocate.
  74.  
  75.   char* file The file that the function is called from.
  76.  
  77.   int line The line number that the function is called from.
  78.   ++++++++++++++++++++++++++++++++++++++*/
  79.  
  80. void* SafeMalloc(unsigned int size,char* file,int line)
  81. {
  82.  void* rptr=malloc(size);
  83.  
  84. #if DEBUG&4
  85.  printf("$$Malloc of %d bytes at %08lx (%s:%3d)\n",size,(long)rptr,file,line);
  86. #endif
  87. #if DEBUG&2
  88.  addresses[malloc_count]=(void*)rptr;
  89.  files[malloc_count]=file;
  90.  lines[malloc_count]=line;
  91. #endif
  92. #if DEBUG
  93.  malloc_count++;
  94.  if(!rptr) printf("$$Warning Malloc() returning NULL (%s:%3d)\n",file,line);
  95. #endif
  96. #if !DEBUG
  97.  if(!rptr) printf("Warning Malloc() returning NULL (%s:%3d)\n",file,line);
  98. #endif
  99.  
  100.  return(rptr);
  101. }
  102.  
  103.  
  104. /*++++++++++++++++++++++++++++++++++++++
  105.   A replacement calloc() function.
  106.  
  107.   void* SafeCalloc Returns the address.
  108.  
  109.   unsigned int n The number of items to allocate.
  110.  
  111.   unsigned int size The size of the memory to allocate.
  112.  
  113.   char* file The file that the function is called from.
  114.  
  115.   int line The line number that the function is called from.
  116.   ++++++++++++++++++++++++++++++++++++++*/
  117.  
  118. void* SafeCalloc(unsigned int n,unsigned int size,char* file,int line)
  119. {
  120.  void* rptr=calloc(n,size);
  121.  
  122. #if DEBUG&4
  123.  printf("$$Calloc of %d bytes at %08lx (%s:%3d)\n",size,(long)rptr,file,line);
  124. #endif
  125. #if DEBUG&2
  126.  addresses[malloc_count]=(void*)rptr;
  127.  files[malloc_count]=file;
  128.  lines[malloc_count]=line;
  129. #endif
  130. #if DEBUG
  131.  malloc_count++;
  132.  if(!rptr) printf("$$Warning Calloc() returning NULL (%s:%3d)\n",file,line);
  133. #endif
  134. #if !DEBUG
  135.  if(!rptr) printf("Warning Calloc() returning NULL (%s:%3d)\n",file,line);
  136. #endif
  137.  
  138.  return(rptr);
  139. }
  140.  
  141.  
  142. /*++++++++++++++++++++++++++++++++++++++
  143.   A replacement realloc() function.
  144.  
  145.   void* SafeRealloc Returns the address.
  146.  
  147.   void* ptr The old pointer.
  148.  
  149.   unsigned int size The size of the new memory to allocate.
  150.  
  151.   char* file The file that the function is called from.
  152.  
  153.   int line The line number that the function is called from.
  154.   ++++++++++++++++++++++++++++++++++++++*/
  155.  
  156. void* SafeRealloc(void* ptr,unsigned int size,char* file,int line)
  157. {
  158.  void* rptr=realloc(ptr,size);
  159.  
  160. #if DEBUG&4
  161.  printf("$$Realloc of %d bytes at %08lx (old %08lx) (%s:%3d)\n",size,(long)rptr,(long)ptr,file,line);
  162. #endif
  163. #if DEBUG&2
  164.  if(rptr!=ptr)
  165.    {int i; for(i=0;i<malloc_count;i++)if(addresses[i]==(void*)ptr) {addresses[i]=rptr;break;}
  166.     if(i==malloc_count) printf("$$Realloc() called for a non Malloced pointer %08lx (%s:%3d)\n",(long)ptr,file,line);}
  167. #endif
  168. #if DEBUG
  169.  realloc_count++;
  170.  if(!rptr) printf("$$Warning Realloc() returning NULL (%s:%3d)\n",file,line);
  171. #endif
  172. #if !DEBUG
  173.  if(!rptr) printf("Warning Realloc() returning NULL (%s:%3d)\n",file,line);
  174. #endif
  175.  
  176.  return(rptr);
  177. }
  178.  
  179.  
  180. /*++++++++++++++++++++++++++++++++++++++
  181.   A replacement free() function.
  182.  
  183.   void* ptr The pointer that is to be freed up.
  184.  
  185.   char* file The file that the function is called from.
  186.  
  187.   int line The line number that the function is called from.
  188.   ++++++++++++++++++++++++++++++++++++++*/
  189.  
  190. void SafeFree(void* ptr,char* file,int line)
  191. {
  192. #if DEBUG&4
  193.  printf("$$Free at %08lx (%s:%3d)\n",(long)ptr,file,line);
  194. #endif
  195. #if DEBUG&2
  196.  {int i; for(i=0;i<malloc_count;i++)if(addresses[i]==(void*)ptr) {addresses[i]=(void*)1;break;} 
  197.   if(i==malloc_count) printf("$$Free() called for a non Malloced pointer %08lx (%s:%3d)\n",(long)ptr,file,line);}
  198. #endif
  199. #if DEBUG
  200.  free_count++;
  201.  if(!ptr) printf("$$Calling Free() on NULL (%s:%3d)\n",file,line);
  202.  else
  203. #endif
  204. #if !DEBUG
  205.  if(!ptr) printf("Calling Free() on NULL (%s:%3d)\n",file,line);
  206.  else
  207. #endif
  208.  
  209.  free(ptr);
  210. }
  211.  
  212.  
  213. /*++++++++++++++++++++++++++++++++++++++
  214.   A function to copy a string on the public global heap.
  215.  
  216.   char* SafeMallocString Returns the copy of the string.
  217.  
  218.   char* x The string to be copied.
  219.  
  220.   char* file The file that the function is called from.
  221.  
  222.   int line The line number that the function is called from.
  223.   ++++++++++++++++++++++++++++++++++++++*/
  224.  
  225. char* SafeMallocString(char* x,char* file,int line)
  226. {
  227.  char* t=NULL;
  228.  unsigned int l=x?strlen(x):0;
  229.  
  230.  if(l)
  231.    {
  232.     t=(char*)SafeMalloc(l+1,file,line);
  233.     strcpy(t,x);
  234.    }
  235.  
  236.  return(t);
  237. }
  238.  
  239.  
  240. /*++++++++++++++++++++++++++++++++++++++
  241.   A function to copy a string on the local private memory heap.
  242.  
  243.   char* CopyString Returns the copy of the string.
  244.  
  245.   char* x The string to be copied.
  246.   ++++++++++++++++++++++++++++++++++++++*/
  247.  
  248. char* CopyString(char* x)
  249. {
  250.  char* t=NULL;
  251.  unsigned int l=x?strlen(x):0;
  252.  
  253.  if(l)
  254.    {
  255.     t=get_space(l+1);
  256.     strcpy(t,x);
  257.    }
  258.  
  259.  return(t);
  260. }
  261.  
  262.  
  263. /*++++++++++++++++++++++++++++++++++++++
  264.   A function to concatenate a number of strings.
  265.  
  266.   char* ConcatStrings Returns the a pointer to the new string.
  267.  
  268.   int n The number of strings
  269.  
  270.   char* s The first string.
  271.  
  272.   ... The other strings, 'n' including 's'.
  273.  
  274.   Any of the strings that are inputs can be NULL, in this case they are quietly ignored.
  275.   ++++++++++++++++++++++++++++++++++++++*/
  276.  
  277. char* ConcatStrings(int n,char* s, ...)
  278. {
  279.  char* t=NULL,*str;
  280.  unsigned int l=0;
  281.  int i;
  282.  va_list ap;
  283.  
  284. #ifdef USE_STD_ARG
  285.  va_start(ap,s);
  286. #else
  287.  va_start(ap);
  288. #endif
  289.  
  290.     for(i=0;i<n;i++)
  291.       {
  292.        if(i)
  293.           str=va_arg(ap, char *);
  294.        else
  295.           str=s;
  296.  
  297.        if(str)
  298.           l+=strlen(str);
  299.       }
  300.  
  301.  va_end(ap);
  302.  
  303.  if(l)
  304.    {
  305.     t=get_space(l+1); t[0]=0;
  306.  
  307. #ifdef USE_STD_ARG
  308.  va_start(ap,s);
  309. #else
  310.  va_start(ap);
  311. #endif
  312.  
  313.     for(i=0;i<n;i++)
  314.       {
  315.        if(i)
  316.           str=va_arg(ap, char *);
  317.        else
  318.           str=s;
  319.  
  320.        if(str)
  321.           strcat(t,str);
  322.       }
  323.  
  324.     va_end(ap);
  325.    }
  326.  
  327.  return(t);
  328. }
  329.  
  330.  
  331. /*++++++++++++++++++++++++++++++++++++++
  332.   Prints out the number of mallocs / reallocs and frees.
  333.   ++++++++++++++++++++++++++++++++++++++*/
  334.  
  335. void PrintMemoryStatistics(void)
  336. {
  337. #if DEBUG
  338.  printf("\n"
  339.         "$$Memory usage : %5d Malloc()/Calloc() calls\n"
  340.         "$$               %5d Realloc() calls\n"
  341.         "$$               %5d Free() calls\n"
  342.         "$$               %5d Net calls (Malloc-Free)\n",
  343.         malloc_count,realloc_count,free_count,malloc_count-free_count);
  344. #endif
  345.  
  346. #if DEBUG&2
  347.  {int i; for(i=0;i<malloc_count;i++) if(addresses[i]!=(void*)1) printf("$$Address %08lx is not freed (%s:%3d) = '%s'\n",(long)addresses[i],files[i],lines[i],(char*)addresses[i]);}
  348. #endif
  349. }
  350.  
  351.  
  352. /*++++++++++++++++++++++++++++++++++++++
  353.   Tidies up the local heap of memory.
  354.   ++++++++++++++++++++++++++++++++++++++*/
  355.  
  356. void TidyMemory(void)
  357. {
  358.  
  359.  if(first)
  360.    {
  361.     Heap h=first,n;
  362.     do{
  363.        n=h->next;
  364.        Free(h->mem);
  365.        Free(h);
  366.        h=n;
  367.       }
  368.     while(h);
  369.    }
  370.  
  371.  first=NULL;
  372.  heap_left=0;
  373. }
  374.  
  375. /*+ The size of each of the heap allocations +*/
  376. #define HEAP_INC 8192
  377.  
  378. /*+ The size of a string that is large enough to have it's own mallocation. +*/
  379. #define SMALL_STRING 256
  380.  
  381. /*++++++++++++++++++++++++++++++++++++++
  382.   A function to get some memory for a string, allocate a new heap structure if needed.
  383.  
  384.   char* get_space Returns a pointer to enough space.
  385.  
  386.   unsigned int l The amount of space that is needed.
  387.   ++++++++++++++++++++++++++++++++++++++*/
  388.  
  389. static char* get_space(unsigned int l)
  390. {
  391.  static Heap current=NULL;
  392.  char* r=NULL;
  393.  
  394.  if(l <= SMALL_STRING)
  395.    {
  396.     if(heap_left < l)
  397.       {
  398.        current=add_to_heap(HEAP_INC);
  399.        heap_left=HEAP_INC;
  400.       }
  401.  
  402.     heap_left-=l;
  403.  
  404.     r=¤t->mem[heap_left]; /* Work downwards */
  405.    }
  406.  else
  407.    {
  408.     Heap h=add_to_heap(l);
  409.     r=h->mem;
  410.    }
  411.  
  412.  return(r);
  413. }
  414.  
  415.  
  416. /*++++++++++++++++++++++++++++++++++++++
  417.   Add some bytes to the privately maintained memory heap.
  418.  
  419.   Heap add_to_heap Returns a pointer to the required memory.
  420.  
  421.   unsigned int l The size of the memory that is required.
  422.   ++++++++++++++++++++++++++++++++++++++*/
  423.  
  424. static Heap add_to_heap(unsigned int l)
  425. {
  426.  Heap* h=&first;
  427.  
  428.  while(*h)
  429.     h=&(*h)->next;
  430.  
  431.  *h=(Heap)Malloc(sizeof(struct _Heap));
  432.  (*h)->next=NULL;
  433.  (*h)->mem=(char*)Malloc(l);
  434.  
  435.  return(*h);
  436. }
  437.